import Vue from "vue";

export function toFix(num, dot) {
  if (num) {
    num = Number(num);
    return num.toFixed(dot);
  } else {
    return 0;
  }
}

export function statusDict(type) {
  switch (type) {
    case 0:
      return "离线";
      break;
    case 1:
      return "正常";
      break;
    case 2:
      return "异常";
      break;
    case 3:
      return "暂停";
      break;
  }
}

// 2017-12-14 15:45:36
export function formatTime(sec) {
  if (sec == null) return "--";
  var date = new Date(sec);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var seconds = date.getSeconds();

  var time = [hour, minute, seconds].map(formatNumber).join(":");
  var date = [year, month, day].map(formatNumber).join("-");

  return date + " " + time;
}

export function formatTime1(sec) {
  if (sec == null) return "--";
  var date = new Date(sec);
  var hour = date.getHours();
  var minute = date.getMinutes();
  var seconds = date.getSeconds();

  var time = [hour, minute, seconds].map(formatNumber).join(":");

  return time;
}

export function toHourMinute(minutes) {
  return Math.floor(minutes / 60) + "小时" + minutes % 60 + "分钟";
}

export function dayLeft(sec) {
  var now = new Date().getTime();

  var diff = sec - now;
  var day = parseInt(diff / (1000 * 60 * 60 * 24)) + 1;
  return day
}

export function nextDampTime(sec) {
  var time = new Date(sec + 24 * 60 * 60 * 1000);
  var day = time.getFullYear()+"-" + (time.getMonth()+1) + "-" + time.getDate() + ' 0:05';
  return day
}


export function shortTime(sec) {
  if (sec == null) return "--";
  var date = new Date(sec);
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var seconds = date.getSeconds();

  var time = [hour, minute].map(formatNumber).join(":");

  return month + "-" + day + " " + time;
}

function formatNumber(n) {
  n = n.toString();
  return n[1] ? n : "0" + n;
}

export function formatRemainTime(timestamp, returnStr) {
  let nowTime = new Date();
  let endTime = new Date(timestamp);
  let t = endTime.getTime() - nowTime.getTime();
  if (t > 0) {
    let day = Math.floor(t / 86400000);
    let hour = Math.floor((t / 3600000) % 24);
    let min = Math.floor((t / 60000) % 60);
    let sec = Math.floor((t / 1000) % 60);
    hour = hour < 10 ? "0" + hour : hour;
    min = min < 10 ? "0" + min : min;
    sec = sec < 10 ? "0" + sec : sec;
    let format = "";
    if (day > 0) {
      format = `${day}天${hour}小时${min}分${sec}秒`;
    }
    if (day <= 0 && hour > 0) {
      format = `${hour}小时${min}分${sec}秒`;
    }
    if (day <= 0 && hour <= 0) {
      format = `${min}分${sec}秒`;
    }
    return format;
  } else {
    return returnStr;
  }
}

export function tradeType(item) {
  var id = item.mid;
  var str = "";
  switch (item.type) {
    case 1:
      // 卖出
      if (item.income == 0) {
        str = "卖出# " + id;
      } else {
        str = "买入# " + id;
      }
      break;
    case 2:
      // 生育
      if (item.oneself) {
        str = "出售# " + id + "生育权";
      } else {
        str = "与# " + id + "进行生育";
      }
      break;
  }
  return str;
}

export function tradeStatus(status) {
  var str = "";
  switch (status) {
    case 0:
      str = "已挂单";
      break;
    case 1:
      str = "等待付款";
      break;
    case 2:
      str = "等待确认";
      break;
    case 3:
      str = "已完成";
      break;
    case -1:
      str = "已取消";
      break;
  }
  return str;
}

export function pointPrefix(type) {
  switch (type) {
    case 1:
    case 3:
    case 7:
    case 12:
    case 14:
    case 16:
    case 18:
    case 21:
    case 23:
    case 24:
    case 26:
    case 27:
    case 29:
    case 30:
      return "+";
      break;

    case 2:
    case 4:
    case 5:
    case 6:
    case 8:
    case 9:
    case 10:
    case 11:
    case 13:
    case 15:
    case 17:
    case 19:
    case 20:
    case 22:
    case 25:
    case 28:
    case 31:
      return "-";
      break;
  }
}
export function coinRecordType(item) {
	var str = "";
	switch (item.type) {
      case 0:
	    str = "充值";
	    break;
      case 1:
  	    str = "提现";
  	    break;
      case 2:
	    str = "挂单扣减";
	    break;
      case 3:
  	    str = "交易取消退款";
  	    break;
      case 4:
	    str = "交易获得";
	    break;
      case 5:
      str = "手续费返利";
      break;
	}
	return str;
}
export function coinRecordTypeFlag(item) {
	switch (item.type) {
      case 0:
      case 3:
      case 4:
        return true;
      case 1:
      case 2:
    	return false;
	}
}

export function recordTypeDict(item) {
  var str = "";
  switch (item.type) {
    case 1:
      str = "充值";
      break;
    case 2:
      str = "提现";
      break;
    case 3:
      str = "掘金收益";
      break;
    case 4:
      str = "出售# " + item.mid + "手续费";
      break;
    case 5:
      str = "与# " + item.mid + "交配手续费";
      break;
    case 6:
      str = "取消扣款";
      break;
    case 7:
      str = "深山修炼奖励";
      break;
    case 8:
      str = "喂食 #" + item.mid;
      break;
    case 9:
      str = "喂食手续费";
      break;
    case 10:
      str = "提现手续费";
      break;
    case 11:
      str = "买道具消费";
      break;
    case 12:
      str = "卖道具收入";
      break;
    case 13:
      str = "买蟠桃消费";
      break;
    case 14:
      str = "卖蟠桃收入";
      break;
    case 15:
      str = "道具交易手续费";
      break;
    case 16:
      str = "出售# " + item.mid + "收入";
      break;
    case 17:
      str = "购买# " + item.mid + "支出";
      break;
    case 18:
      str = "出售# " + item.mid + "生育收入";
      break;
    case 19:
      str = "购买# " + item.mid + "生育支出";
      break;
    case 20:
      str = "抽奖";
      break;
    case 21:
      str = "猴毛收益";
      break;
    case 22:
      str = "采集支出";
      break;
    case 23:
      str = "城堡收益";
      break;
    case 24:
      str = "抽奖收益";
      break;
    case 25:
      str = "增值服务费";
      break;
    case 26:
      str = "排行榜奖励";
      break;
    case 27:
      str = "攻城奖励";
      break;
    case 28:
      str = "挂单扣减";
      break;
    case 29:
      str = "取消挂单";
      break;
    case 30:
      str = "挂单成交";
      break;
    case 31:
      str = "成交手续费";
      break;
  }
  return str;
}

export function withDrawStatus(item) {
  var str = "";
  if (item.type == 2) {
    if (item.status == 0) {
      str = "处理中";
    } else {
      str = "已完成";
    }
  }
  return str;
}

export function min(num) {
  if (num == 0) {
    return 0;
  }
  if (num < 1 && num > 0) {
    return 1;
  }
  num = Math.floor(num);
  return num;
}

export function strKW(num) {
  if(num < 1000) {
    return num;
  } else if(num >=1000 && num < 10000){
    return (num/1000).toFixed(1)+"K";
  } else if (num >= 10000) {
    return (num/10000).toFixed(2)+"W";
  }
}

const filters = {
  toFix,
  formatTime,
  formatRemainTime,
  tradeType,
  tradeStatus,
  pointPrefix,
  recordTypeDict,
  withDrawStatus,
  formatTime1,
  shortTime,
  min,
  toHourMinute,
  dayLeft,
  coinRecordType,
  coinRecordTypeFlag,
  strKW,
  nextDampTime
};

export default filters;

Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key]);
});
